﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Globalization;
using HREDU.Hoang_Tuan.Classes;
using HREDU.Hoang_Tuan;


namespace HREDU.Hoang_Tuan
{
    public partial class frmSearchCommon : Form
    {
        public string strGroup;
        public int strIdStaff =-1;
        public Form fCallForm;
        private ArrayList objArrSearchCode = new ArrayList();
        private ArrayList objArrStaff = new ArrayList();
        private string strFieldName = "";
        
        public frmSearchCommon()
        {
            InitializeComponent();
        }

        public frmSearchCommon(Form frmCall)
        {
            InitializeComponent();
            fCallForm = frmCall;
        }

        private void frmSearchCommon_Load(object sender, EventArgs e)
        {
            // Lay thong tin bang SearchCode 
            getArrSearchCode();
            CSearch objSearch = new CSearch();

            cmbName.Items.Clear();
            //Load thong tin vao Combobox
            //for (int i = 0; i < objArrSearchCode.Count ; i++)
            //{
            //    objSearch = (CSearch)(objArrSearchCode[i]);
            //    cmbName.Items.Add(objSearch.Fieldname.ToString().Trim());
            //}
            cmbName.DataSource = objArrSearchCode;
            cmbName.DisplayMember = "FIELDNAME";
            cmbName.ValueMember = "FIELDTYPE";
        }
       
        private void getArrSearchCode()
        {
            //Lay thong tin SearchCode dua vao mang 
            DataSet objDataSet;
            string strSQL;
            int iCount;
            strSQL = "select * from search where SEARCHCODE ='" + strGroup + "' order by POSITION ";
            
            //Lay du lieu dua vao DataSet 
            objDataSet = MITI.BaseDB.Instance.ExecSql_DataSet(strSQL);
            
            //Duyet DataSet dua vao mang 
            objArrSearchCode.Clear();
            iCount = 0;

            if (objDataSet.Tables[0].Rows.Count != 0)
            {
                iCount = objDataSet.Tables[0].Rows.Count;
                for (int i = 0; i < iCount; i++)
                {
                    CSearch objSearch = new CSearch();
                    objSearch.Fieldcode = objDataSet.Tables[0].Rows[i]["FIELDCODE"].ToString();
                    objSearch.Fieldname = objDataSet.Tables[0].Rows[i]["FIELDNAME"].ToString();
                    objSearch.Fieldtype = objDataSet.Tables[0].Rows[i]["FIELDTYPE"].ToString();
                    objSearch.Searchcode = objDataSet.Tables[0].Rows[i]["SEARCHCODE"].ToString();
                    objSearch.Display = objDataSet.Tables[0].Rows[i]["DISPLAY"].ToString();
                    objSearch.Format = objDataSet.Tables[0].Rows[i]["FORMAT"].ToString();
                    objSearch.Operator = objDataSet.Tables[0].Rows[i]["OPERATOR"].ToString();
                    objArrSearchCode.Add(objSearch);
                }
            }

        }

        private void cmbName_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
             * Dungnv : 27/08/2008 
             */
            
            string strFormular;
            
            CSearch objSearch = new CSearch();
                strFormular = "";
                strFieldName = cmbName.Text.ToString().Trim();

                for (int i = 0; i < objArrSearchCode.Count; i++)
                {
                    objSearch = (CSearch)(objArrSearchCode[i]);
                    if (strFieldName == objSearch.Fieldname.ToString().Trim())
                    {
                        strFormular = objSearch.Operator.ToString().Trim();
                        strFieldName = objSearch.Fieldcode .ToString().Trim();
                        break;
                    }
                }

            //Xu ly tach chuoi 
            if (strFormular != "")
            {

                string[] arr;
                arr = strFormular.Split(',') ;
                cmbformula.Items.Clear();
                foreach (string str in arr)
                {
                    if (str != "")
                    {
                        cmbformula.Items.Add(str.Trim());
                    }
                }

            }
   
        }

        private void bttAdd_Click(object sender, EventArgs e)
        {
            /*
             * Dungnv : 27/08/2008 
             * Add dieu kien tim kiem 
             */
            string strConditionFind = ""; //Dieu kien duoc chuyen doi thanh cau lenh cho SQL
            //string strConditionShow = ""; //Dieu kien hien thi cho nguoi dung
            

            if ((txtContent.Text.ToString().Trim() != "") &  (cmbName.Text.ToString().Trim() !="") & (cmbformula.Text.ToString().Trim() !=""))    
            {
                if (cmbName.SelectedValue.ToString() == "C")
                {
                    if (cmbformula.Text.ToString().Trim() == "LIKE %X%")
                    {
                        strConditionFind = strFieldName + " " + "LIKE N'%" + txtContent.Text.ToString().Trim() + "%'";
                    }
                    else if (cmbformula.Text.ToString().Trim() == "LIKE %X")
                    {
                        strConditionFind = strFieldName + " " + "LIKE N'%" + txtContent.Text.ToString().Trim() + "'";
                    }
                    else if (cmbformula.Text.ToString().Trim() == "LIKE X%")
                    {
                        strConditionFind = strFieldName + " " + "LIKE N'" + txtContent.Text.ToString().Trim() + "%'";
                    }
                    else
                    {
                        strConditionFind = strFieldName + " " + cmbformula.Text.ToString().Trim() + "N'" + txtContent.Text.ToString().Trim() + "'";
                    }
                }
                else if (cmbName.SelectedValue.ToString() == "DT")
                {
                    DateTime dt = DateTime.Parse(txtContent.Text);//,culInf, DateTimeStyles.NoCurrentDateDefault);
                    CultureInfo cul = GeneralCode.GetCultureInfoUS();
                    DateTimeFormatInfo dfi = cul.DateTimeFormat;

                    strConditionFind = strFieldName + " " + cmbformula.Text.ToString().Trim() + "'" + dt.ToString(dfi.ShortDatePattern) + "'";
                }
                checkList.Items.Add(strConditionFind.ToString().Trim());  
                
            }
            else
            {
                MessageBox.Show("Phải nhập nội dung tìm kiếm");  
            }
        }

        private void bttRemove_Click(object sender, EventArgs e)
        {
            /* Dungnv : 31/08/2008
             * Xoa muc chon dieu kien 
             */
            foreach(int  i  in checkList.CheckedIndices )
            {
                checkList.Items.RemoveAt(i);         
            }
        }

        private void cmdSearch_Click(object sender, EventArgs e)
        {
            /*
             * Dungnv : 31/08/2008 
             * Xu ly Search Tim Kiem
             */
            
            //Lay danh sach  cac thong tin duoc chon 
            //DataSet objDataSet;
            string strConditon = "";
            foreach (int i in checkList.CheckedIndices)
            {
                if (strConditon == "")
                {
                    strConditon = strConditon + checkList.Items[i].ToString().Trim();
                }
                else
                {
                    strConditon = strConditon + " AND " + checkList.Items[i].ToString().Trim();
                }
            }
            
            if (fCallForm != null)
            {
                if (fCallForm.Name == "frmDSNHANSU")
                {
                    frmDSNHANSU frm = (frmDSNHANSU)fCallForm;
                    frm.strSearchCond = strConditon;
                    frm.AddDK();
                    frm.LoadDataGridJanusDSNhanSu();
                }
            }
        }
    }
}